﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

namespace MyVocationSpace
{
    public partial class ViewProfile : System.Web.UI.Page
    {
        public int CurrentPage = 0;
        public int totalPage;
        public int pageNo = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            BAL.conn.connectionString = ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString;
            if (Session["UserID"] != null)
            {
                if (Request.QueryString["IDUser"] != null)
                {
                    int IdUser = Convert.ToInt32(Request.QueryString["IDUser"]);
                    ViewState["IdUser"] = IdUser;
                    GetData(IdUser);                    
                    PopulateTopic(IdUser);
                    GetHigherCourse(IdUser);
                    GetCurrentProfile(IdUser);
                    
                }
                else
                {
                    GetData(Convert.ToInt32(Session["UserID"]));
                    PopulateTopic(Convert.ToInt32(Session["UserID"]));
                    GetHigherCourse(Convert.ToInt32(Session["UserID"]));
                    GetCurrentProfile(Convert.ToInt32(Session["UserID"]));
                }
            }
            else
            {
                Response.Redirect("Home.aspx");
            }
        }

        public void GetData(int Iduser)
        {
           ViewState["Iduser"]=Iduser;
            string[] Detail = BAL.Users.GetUserDetails(Iduser);
            username.InnerHtml = Detail[1] + " " + Detail[2];
            Occu.InnerHtml = Detail[0];
            city.InnerHtml = Detail[7];
            country.InnerHtml = Detail[16];
            current.InnerHtml = Detail[0];
            industry.InnerHtml = Detail[18];
            education.InnerHtml = Detail[20];           
            org.InnerHtml = Detail[14];
            industryMain.InnerHtml = Detail[18];

            lblName.Text = Detail[1];

            string img = Detail[19];
            if (img != "")
            {
                idImg.Src = "UsersPhoto/" + img;
            }
            else
            {
                idImg.Src = "UsersPhoto/icon_no_photo_60x60.png";
            }

            int count = BAL.Users.GetConnectionByUser(Iduser);
            connect.Text = count.ToString() + " " + "<font style=\"color:#1C6F93; font-size:12px; font-weight:normal;\">connections</font>";


            DataSet ds = BAL.Users.GetVocationByUser(Iduser);
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //phVocation.Controls.Add(new LiteralControl("<table width=\"350px\">"));
                    //    foreach(DataRow dr in ds.Tables[0].Rows)
                    //    {
                    //        phVocation.Controls.Add(new LiteralControl("<tr><td align=\"left\">" + dr["VocationName"] + "</td></tr>"));
                    //    }
                    //phVocation.Controls.Add(new LiteralControl("</table>"));

                    phVocation.Controls.Add(new LiteralControl("<ul width=\"350px\" style=\"margin-top:-0px; margin-left:-22px\">"));
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        phVocation.Controls.Add(new LiteralControl("<li align=\"left\">" + dr["VocationName"] + "</li>"));
                    }
                    phVocation.Controls.Add(new LiteralControl("</ul>"));
                }
            }
        }

        public void PopulateTopic(int IDUser)
        {
            DataSet ds = BAL.Topics.GetTopicByUser(IDUser);

            PagedDataSource pgds = new PagedDataSource();
            pgds.DataSource = ds.Tables[0].DefaultView;

            int noOfItem = pgds.Count;
            pgds.AllowServerPaging = true;
            pgds.PageSize = 5;

            totalPage = pgds.PageCount;
            ViewState["TotalPage"] = totalPage;

            pgds.CurrentPageIndex = CurrentPage;
            ViewState["CurrentPage"] = CurrentPage;

            lnkNext.Visible = !pgds.IsLastPage;
            lnkPre.Visible = !pgds.IsLastPage;

            int currpageShow = Convert.ToInt32(ViewState["CurrentPage"]) + 1;
            Label1.Text = "Page " + currpageShow.ToString() + " of " + ViewState["TotalPage"].ToString();
            Label2.Text = noOfItem + " items found. Showing upto 10 items per page.";

            if (pgds != null)
            {
                dlMyTopic.DataSource = pgds.DataSource;
                dlMyTopic.DataBind();
            }
        }

        protected void lnkPre_Click(object sender, EventArgs e)
        {
            CurrentPage = Convert.ToInt32(ViewState["CurrentPage"]);
            CurrentPage += 1;
            pageNo = Convert.ToInt32(ViewState["CurrentPage"]);
            pageNo += 1;
            PopulateTopic(Convert.ToInt32(Session["UserID"]));
        }

        protected void lnkNext_Click(object sender, EventArgs e)
        {
            CurrentPage = Convert.ToInt32(ViewState["CurrentPage"]);
            CurrentPage -= 1;
            pageNo = Convert.ToInt32(ViewState["CurrentPage"]);
            pageNo -= 1;
            PopulateTopic(Convert.ToInt32(Session["UserID"]));
        }

        protected void connect_Click(object sender, EventArgs e)
        {
            Response.Redirect("Connections.aspx?IDUser= " + Convert.ToInt32(ViewState["Iduser"]) + "");
        }


        public void GetHigherCourse(int IDUser)
        {
            DataSet ds = BAL.Users.GetHigherCourseByUserID(IDUser);

            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    phEducation.Controls.Add(new LiteralControl("<table width=440px;>"));
                    phEducation.Controls.Add(new LiteralControl("<tr><td style=\"width=440px; font-famaly:Arial;font-size:15px; vertical-align: middle;  padding: 3px 0px 0px 3px; font-weight:bold;color:#1C6F93;background-color:#F4F4F4;\">Education<hr style=\"width: 440px; text-align: left; border-top: 3px double #1C6F93;\"/></td></tr>"));
                    phEducation.Controls.Add(new LiteralControl("<tr><td style=\"padding-left:15px;\"><table width=420px;>"));
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        phEducation.Controls.Add(new LiteralControl("<tr><td align=\"right\" style=\"color:#747474;width:150px;\">Higher Education:</td><td> " + dr["Course"].ToString() + "</td></tr>"));
                        phEducation.Controls.Add(new LiteralControl("<tr><td align=\"right\" style=\"color:#747474;\">Institute:</td><td> " + dr["College"].ToString() + "</td></tr>"));
                        phEducation.Controls.Add(new LiteralControl("<tr><td align=\"right\" style=\"color:#747474;\">Year:</td><td> " + dr["Year"].ToString() + "</td></tr>"));
                    }
                    phEducation.Controls.Add(new LiteralControl("</table>"));
                    phEducation.Controls.Add(new LiteralControl("</td></tr></table>"));
                }
            }
        }

        public void GetCurrentProfile(int IDUser)
        {
            DataSet ds = BAL.Users.GetCurrentExpByUserID(IDUser);

            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    phEducation.Controls.Add(new LiteralControl("<table width=440px;>"));
                    phEducation.Controls.Add(new LiteralControl("<tr><td style=\"width=440px; font-famaly:Arial;font-size:15px; font-weight:bold;color:#1C6F93;background-color:#F4F4F4;\">Current Profile<hr style=\"width: 440px; text-align: left; border-top: 3px double #1C6F93;\"/></td></tr>"));
                    phEducation.Controls.Add(new LiteralControl("<tr><td style=\"padding-left:15px;\"><table width=420px;>"));
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        phEducation.Controls.Add(new LiteralControl("<tr><td align=\"right\" style=\"color:#747474;width:150px;\">Designation:</td><td> " + dr["Designation"].ToString() + "</td></tr>"));
                        phEducation.Controls.Add(new LiteralControl("<tr><td align=\"right\" style=\"color:#747474;\">Orgnization:</td><td> " + dr["Orgnization"].ToString() + "</td></tr>"));
                        phEducation.Controls.Add(new LiteralControl("<tr><td align=\"right\" style=\"color:#747474;\">Location:</td><td> " + dr["Location"].ToString() + "</td></tr>"));
                        phEducation.Controls.Add(new LiteralControl("<tr><td align=\"right\" style=\"color:#747474;\">Year From:</td><td> " + dr["YearFrom"].ToString() + "</td></tr>"));
                        phEducation.Controls.Add(new LiteralControl("<tr><td align=\"right\" style=\"color:#747474;\">Year To:</td><td> " + dr["YearTo"].ToString() + "</td></tr>"));
                    }
                    phEducation.Controls.Add(new LiteralControl("</table>"));
                    phEducation.Controls.Add(new LiteralControl("</td></tr></table>"));
                }
            }
        }


    }
}